﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace BUS
{
    public class CInputKeyRunningManual
    {
        ListBox _listKey;

        public CInputKeyRunningManual()
        {
            _listKey = new ListBox();
            _listKey.Visible = false;
            _listKey.Size = new Size(100, 100);
            _listKey.Sorted = true;
        }

        public void Install(ref RichTextBox richTextBox)
        {
            richTextBox.KeyDown += new KeyEventHandler(KeyDown);
            richTextBox.KeyUp += new KeyEventHandler(KeyUp);
            richTextBox.KeyPress += new KeyPressEventHandler(KeyPress);
            richTextBox.TextChanged+=new EventHandler(TextChanged);
        }

        public void Uninstall(ref RichTextBox richTextBox)
        {
            richTextBox.KeyDown -= new KeyEventHandler(KeyDown);
            richTextBox.KeyUp -= new KeyEventHandler(KeyUp);
            richTextBox.KeyPress -= new KeyPressEventHandler(KeyPress);
            richTextBox.TextChanged -= new EventHandler(TextChanged);
        }

        public void TextChanged(object sender, EventArgs e)
        {
            RichTextBox _rtxt = (RichTextBox)sender;
            int _start=_rtxt.SelectionStart;
            int _leangth=_rtxt.SelectionLength;

            bool _flag = false;
            if (_rtxt.Text.Length > 0)
            {
                for (int i = _rtxt.SelectionStart-1; i >= 0; i--)
                {
                    if (_rtxt.Text[i] == '<' || _rtxt.Text[i] == '(')
                    {
                        _listKey.Parent = _rtxt;
                        
                        _listKey.Location = new Point(100, 100);
                        _listKey.Items.Clear();
                        string[] _temp = BUS.CTransferKeysCode.ListKeyWithString(BUS.CEditString.GetString(_rtxt.Text, i + 1, _start - i));
                        for (int j = 0; j < _temp.Length; j++)
                            _listKey.Items.Add(_temp[j]);
                        if (_listKey.Items.Count > 1)
                        {
                            _listKey.SelectedIndex = 0;
                            _listKey.Show();
                            _rtxt.Focus();
                            _flag = true;
                        }
                        break;
                    }
                    else if (_rtxt.Text[i] == '<' || _rtxt.Text[i] == '(')
                    {
                        _flag = false;
                        break;
                    }
                }
                if (!_flag)
                    _listKey.Hide();
            }
        }


        bool _Select;
        public void KeyDown(object sender, KeyEventArgs e)
        {
            RichTextBox _rtxt = (RichTextBox)sender;
            _Select = false;
            if (_rtxt.SelectionStart > 0)
            {
                if (_rtxt.Text[_rtxt.SelectionStart - 1] == '<' || (e.Shift&&e.KeyCode == Keys.Oemcomma))
                {
                    _rtxt.SelectionColor = Color.SkyBlue;
                }
                else if (_rtxt.Text[_rtxt.SelectionStart - 1] == '(' || (e.Shift&&e.KeyCode == Keys.D0))
                {
                    _rtxt.SelectionColor = Color.Blue;
                }
                else if (_rtxt.Text[_rtxt.SelectionStart - 1] == ')' || _rtxt.Text[_rtxt.SelectionStart - 1] == '>')
                {
                    _rtxt.SelectionColor = Color.Black;
                }
            }
            else
                _rtxt.SelectionColor = Color.Black;
            if (_listKey.Visible)
            {
                if (e.KeyCode == Keys.Down)
                {
                    if (_listKey.Items.Count - 1 == _listKey.SelectedIndex)
                    {
                        _listKey.SelectedIndex = 0;
                    }
                    else
                        _listKey.SelectedIndex++;
                    _Select = true;
                }
                else if (e.KeyCode == Keys.Up)
                {
                    if (_listKey.SelectedIndex == 0)
                    {
                        _listKey.SelectedIndex = _listKey.Items.Count - 1;
                    }
                    else
                        _listKey.SelectedIndex--;
                    _Select = true;
                }
                else if (e.KeyCode == Keys.Left || e.KeyCode == Keys.Right)
                {
                    _listKey.Hide();
                }
                else if (e.KeyCode == Keys.Return)
                {
                    _Select = true;
                }
                if (_Select)
                    e.Handled = true;
            }
        }

        public void KeyUp(object sender, KeyEventArgs e)
        {
            if (_listKey.Visible)
            {
                if (e.KeyCode == Keys.Return)
                {
                    RichTextBox _rtxt = (RichTextBox)sender;
                    int _start = _rtxt.SelectionStart;
                    _rtxt.Text=_rtxt.Text.Insert(_start, _listKey.SelectedItem.ToString());
                    _rtxt.SelectionStart = _start + _listKey.SelectedItem.ToString().Length;
                    _listKey.Hide();
                    _Select = true;
                }
            }
            if (_Select)
                e.Handled = true;
        }

        public void KeyPress(object sender, KeyPressEventArgs e)
        {
            RichTextBox _rtxt = (RichTextBox)sender;
            if (e.KeyChar == '<')
            {
                _rtxt.SelectionColor = Color.SkyBlue;
            }
            else if (e.KeyChar == '(')
            {
                _rtxt.SelectionColor = Color.Blue;
            }
            if (_Select)
                e.Handled = true;
        }
    }
}
